package org.testis.communicationmodule.services;

import org.testis.communication.client.AtofUpnpClient;
import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import java.util.LinkedHashMap;


/**
 * A class to define an upnp client action for
 * Communication module
 * @author nurhat
 */
public class UpnpClientAction extends AtofModuleAction
{

    /**
     * Check if  device with given serial number is connected to 
     * current pc's interface or not by using AtofUpnpClient.
     * @param request
     * @return
     */
    @Override
    public boolean operateRequest(AtofModuleService request)
    {
        LinkedHashMap<String, String> moduleParams = request.getDataObj().
            getModuleDataToApply();
        LogSenderService.putMessageOnQueue(
            "UpnpClientAction",
            AtofLogLevel.INFO,
            "Starting to execute upnp client action ....",
            request.getDataObj().getMessageId());
        boolean result = false;
        if (moduleParams == null) {
            throw new TestStepException(
                TestStepResultType.FAILURE,
                "Execution of UpnpClientAction failed. Module parameters not given...");
        }
        String deviceType = moduleParams.get("DEVICE_TYPE");
        String serialNo = moduleParams.get("DEVICE_SERIAL_NUMBER");
        if (deviceType == null) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Execution of UpnpClientAction failed. Device type Module parameter not given...");
        }
        if (serialNo == null) {
            throw new TestStepException(
                TestStepResultType.MISTAKE,
                "Execution of UpnpClientAction failed. Serial Number Module parameter not given...");
        }
        AtofUpnpClient client = new AtofUpnpClient();

        result = client.checkIfDeviceExist(deviceType,
                                           serialNo);
        if (result) {
            LogSenderService.putMessageOnQueue(
                "UpnpClientAction",
                AtofLogLevel.INFO,
                "Device with serial number " + serialNo + " was found...",
                request.getDataObj().getMessageId());
        }
        else {
            LogSenderService.putMessageOnQueue(
                "UpnpClientAction",
                AtofLogLevel.INFO,
                "Device with serial number " + serialNo +
                " was not found...",
                request.getDataObj().getMessageId());
        }
        AtofLogLevel level = result ? AtofLogLevel.INFO : AtofLogLevel.ERROR;
        LogSenderService.putMessageOnQueue(
            "UpnpClientAction",
            level,
            "UpnpClientAction executed " + (result ? "" : "un") +
            "successfully...",
            request.getDataObj().getMessageId());
        return result;
    }
}
